<template>
  <el-dialog
    v-model="show" width="650px" :title="title" append-to-body :close-on-click-modal="false"
  >
    <el-form ref="foodRef" :model="form" :rules="rules" label-width="120px" :disabled="isView">
      <el-form-item label="食品图片" prop="photos">
        <image-upload2 v-model="form.photos" :limit="3" :disabled="isView" />
      </el-form-item>
      <el-form-item label="餐别" prop="category" style="width: 80%">
        <el-radio-group v-model="form.category">
          <el-radio :label="0">早餐</el-radio>
          <el-radio :label="1">午餐</el-radio>
          <el-radio :label="2">晚餐</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="食品名称" prop="name" style="width: 80%">
        <el-input v-model="form.name" placeholder="请输入食品名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!isView" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import { getCurrentInstance, ref, toRefs, watch } from 'vue'
import useVenStore from '@/store/modules/ven'
import { reactive } from '@vue/runtime-core'
import { addFood, updateFood } from '@/api/logex/food'

const { proxy } = getCurrentInstance()
const emit = defineEmits(['submit'])

const show = ref(false)
const title = ref('')
const buttonLoading = ref(false)
const isView = ref(false)

const data = reactive({
  form: {
    id: null,
    venueId: null,
    restaurantId: null,
    photos: null,
    category: null,
    name: null,
    status: '1',
    delStatus: '0'
  },
  rules: {
    photos: [
      { required: true, message: '食品图片不能为空', trigger: 'blur' }
    ],
    category: [
      { required: true, message: '餐别', trigger: 'change' }
    ],
    name: [
      { required: true, message: '食品名称', trigger: 'blur' }
    ]
  }
})
const { form, rules } = toRefs(data)

function submitForm() {
  proxy.$refs['foodRef'].validate(valid => {
    if (valid) {
      buttonLoading.value = true
      if (form.value.id != null) {
        updateFood(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          show.value = false
          emit('submit', form.value.restaurantId)
        }).finally(() => {
          buttonLoading.value = false
        })
      } else {
        addFood(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          show.value = false
          emit('submit', form.value.restaurantId)
        }).finally(() => {
          buttonLoading.value = false
        })
      }
    }
  })
}

watch(show, (val) => {
  if (!val) {
    reset()
  }
})

function reset() {
  form.value = {
    id: null,
    venueId: null,
    restaurantId: null,
    photos: null,
    category: null,
    name: null,
    status: '1',
    delStatus: '0'
  }
  proxy.resetForm('foodRef')
}

function open(params) {
  reset()

  show.value = true
  title.value = params.title
  isView.value = params.isView || false

  if (params.food) {
    form.value = params.food
  } else {
    form.value.venueId = useVenStore().venueInfo.id
    form.value.restaurantId = params.restaurantId
  }
}
defineExpose({
  open
})
</script>

<style scoped>
.mx-1{margin: 5px}
</style>
